package com.liuaq.liucomputer;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.view.Window;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class BaseActivity extends AppCompatActivity {
    public boolean mytheme;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        //因为这是API23之后才能改变的，所以你的判断版本
        super.onCreate(savedInstanceState);
        setSysStatus(getColor(R.color.gray2),false);

        //主题用SharedPreferences保存下来，这样即使下次打开后还是上一次切换后的主题。
        SharedPreferences sharedPreferences = getSharedPreferences("info", MODE_PRIVATE);
        mytheme = sharedPreferences.getBoolean("ischecked", false);
        if (!mytheme) {
            setTheme(R.style.WhiteTheme);
            setSysStatus(getColor(R.color.gray2),mytheme);
        } else {
            setTheme(R.style.BlackTheme);
            setSysStatus(getColor(R.color.black3),mytheme);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mytheme != getSharedPreferences("info", MODE_PRIVATE).getBoolean("ischecked", false)) {
            changetheme();
        }
    }

    protected void changetheme() {
        Intent intent = getIntent();
        Log.d("test", "changetheme: " + intent);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所到的界面中间的activity
        // intent.putExtra()
        startActivity(intent);
        //渐出与渐入动画
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);//设置进入退出动画
        finish();
    }

    public void setSysStatus(int color, boolean mytheme) {
        //获取窗口区域
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!mytheme)
                //设置显示为白色背景，黑色字体
                window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            else getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE); //白色字体
            //设置状态栏颜色
            window.setStatusBarColor(color);
        }
    }

}
